package servlet;

import dao.OrderDAO;
import dao.UserDAO;
import dao.UserDAO1;
import entity.Order;
import entity.PageBean;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：
 * 作者：dy
 * 日期：2024/7/15 15:47
 */
@WebServlet(name="orderList", value = "/order/list")
public class OrderListAllServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        UserDAO1 userDAO1 = new UserDAO1();
        User user = userDAO1.findByName(username);

        //1. 接受pageNum=1 pageSize=10
        int pageNum = 1;
        int pageSize = 10;
        //2. 将pageNum pageSize === skip 和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
        //3. 调用productDAO查询数据 list
        OrderDAO orderDAO = new OrderDAO();
        List<Order> list;
        long total;
        if("admin".equals(username)){
            list = orderDAO.findPage(skip, limit,null);
            total = orderDAO.count(null);
        }else{
            list = orderDAO.findPage(skip, limit,user.getId());
            total = orderDAO.count(user.getId());
        }
        //4. 将total list pageNum pageSize 封装--->PageBean 传给List.jsp
        PageBean<Order> page = new PageBean<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(total);
        page.setPageTotal(total,pageSize);
        req.setAttribute("page",page);
        //5. 跳转
        req.getRequestDispatcher("/order/List.jsp").forward(req,resp);
    }
}
